package com.open.api.peekapi.services;

import com.google.common.collect.Lists;
import com.open.api.peekapi.bean.ApiItemData;
import com.open.api.peekapi.dao.ApiItemDataDao;
import com.open.api.peekapi.util.HelpMe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by mengdexuan on 2017/6/21 9:18.
 */
@Service
public class ApiItemDataServices {

	@Autowired
	ApiItemDataDao apiItemDataDao;



	public List<ApiItemData> data(String itemId){

		List<ApiItemData> list = apiItemDataDao.findByItemIdOrderByCreateTime(itemId);

		return list;
	}


	public ApiItemData getOne(String id){
		return apiItemDataDao.findOne(id);
	}


	public ApiItemData add(ApiItemData apiItemData){

		ApiItemData result = null;
		if (HelpMe.isNull(apiItemData.getId())){
			apiItemData.setId(HelpMe.uuid());
			apiItemData.setCreateTime(new Date());
			result = apiItemDataDao.save(apiItemData);
		}else {
			ApiItemData old = apiItemDataDao.getOne(apiItemData.getId());
			HelpMe.copyProperties(old, apiItemData, Lists.newArrayList("id", "itemId", "createTime"));
			result = apiItemDataDao.save(old);
		}

		return result;
	}




	public void del(String id){

		apiItemDataDao.delete(id);

	}






}























